home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacPeople 2001 June 15
/
MACPEOPLE-2001-06-15.ISO.7z
/
MACPEOPLE-2001-06-15.ISO
/
連載データ
/
OS X情報局
/
x-assist-03j.sit
/
X-Assist Japanese.dmg
/
X-Assist プラグイン SDK
/
MP3 Plugin Example
/
XAPlugin.m
< prev
Wrap
Text File
|
2001-04-17
|
7KB
|
291 lines
//
// Plugin.m
// MyPlugin
//
// Created by rosst on Fri Apr 13 2001.
// Copyright (c) 2001 Ross Tulloch. All rights reserved.
//
#import "XAPlugin.h"
#include <Carbon/Carbon.h>
// My defines.
#define kNumberOfDefaultMenuItems 2
#define kFirstTrackItem kNumberOfDefaultMenuItems + 1
// ============================
// == Change "MP3Plugin" below to your plugin name.
// == Once you build the plugin, place it into the "X-Assist Plugins" folder
// == and everything should just load.
// == NOTE: you MUST use a unique name, otherwise the conflicting plugins
// == will NOT load.
// ============================
@implementation MP3Plugin
- (id)init
{
// init plugin internals...
mPluginMenuItem = NULL;
// ===============================
// Your Initialization code here...
// ===============================
mPlaybackFolder = NULL;
mCurMovie = NULL;
mPlaybackItem = 0;
EnterMovies();
// END Initialization code.
return self;
}
- (void)shutdownPlugin
{
// ===============================
// Your Shutdown code here...
// ===============================
[self Cleanup];
if (mPlaybackFolder != NULL)
[mPlaybackFolder release];
if (mPluginMenuItem != NULL)
[mPluginMenuItem release];
}
// Required Plugin Routines
- (NSMenuItem*)getMenuItem
{
if (mPluginMenuItem == NULL)
{
mPluginMenuItem = [[NSMenuItem alloc] init];
// ===============================
// Setup our Menu item to our specifications...
// ===============================
{
NSMenuItem* anItem = NULL;
mSubMenu = [[NSMenu alloc] init];
[mPluginMenuItem setTitle:@"MP3"];
anItem = [[NSMenuItem alloc] init];
[anItem setTitle:@"Choose Folderノ"];
[anItem setEnabled:YES];
[anItem setAction:@selector(ChooseFolder:) ];
[anItem setTarget:self];
[mSubMenu addItem:anItem];
[anItem autorelease];
anItem = [[NSMenuItem alloc] init];
[anItem setTitle:@"Stop Playing"];
[anItem setEnabled:YES];
[anItem setAction:@selector(StopPlaying:) ];
[anItem setTarget:self];
[mSubMenu addItem:anItem];
[anItem autorelease];
[mSubMenu addItem:[NSMenuItem separatorItem]];
[mPluginMenuItem setSubmenu:mSubMenu];
[mSubMenu autorelease];
}
// END setup.
// ===============================
}
return mPluginMenuItem;
}
// items below are for future use.
- (int)getMenuItemInsertLocation
{
return 0; // default location.
}
- (int)getPluginAPIVersion
{
return 1;
}
// return info & copyright etc.
- (NSString*)getPluginDescription
{
return @"MP3Plugin v0.1¥r¥rSimple MP3 Player by Ross Tulloch.";
}
// put up config window...
- (void)doConfigurePlugin
{
}
// User routines
- (IBAction)ChooseFolder:(id)sender
{
int result;
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES ];
[oPanel setCanChooseDirectories:YES];
[oPanel setCanChooseFiles:NO];
// Should add this later....
[oPanel setAllowsMultipleSelection:NO];
result = [oPanel runModalForDirectory:NSHomeDirectory() file:nil types:NULL];
if (result == NSOKButton)
{
NSArray *filesToOpen = [oPanel filenames];
NSString* aFile = [filesToOpen objectAtIndex:0];
NSFileManager* oFS = [NSFileManager defaultManager];
mPlaybackFolderPath = aFile;
if (mPlaybackFolder != NULL)
[mPlaybackFolder release];
mPlaybackFolder = [oFS directoryContentsAtPath:aFile];
[mPlaybackFolder retain];
// Load file names into menu...
[self UpdateMenus];
// Start playing....
[self PlayMenuItemNumber:kFirstTrackItem];
}
}
- (IBAction)StopPlaying:(id)sender
{
// Stop..
[self Cleanup];
}
- (void)UpdateMenus
{
// Remove old items...
{
int menuItemCount = [mSubMenu numberOfItems];
while ( --menuItemCount > kNumberOfDefaultMenuItems )
[mSubMenu removeItemAtIndex:menuItemCount];
}
// Add new items...
{
int i, count = [mPlaybackFolder count];
for (i=0; i<count; i++) {
NSMenuItem* anItem = [[NSMenuItem alloc] init];
[anItem setTitle:[mPlaybackFolder objectAtIndex:i]];
[anItem setEnabled:YES];
[anItem setAction:@selector(PlayMeMenuSelect:) ];
[anItem setTarget:self];
[mSubMenu addItem:anItem];
[anItem autorelease];
}
}
}
- (IBAction)PlayMeMenuSelect:(id)sender
{
// Play the item number...
[self PlayMenuItemNumber:[mSubMenu indexOfItem:sender] ];
}
-(void)PlayMenuItemNumber:(int)itemNum
{
NSMenuItem* oSelected = [mSubMenu itemAtIndex:itemNum];
// UGLY UGLY UGLY
char szString[4096];
strcpy( szString, [mPlaybackFolderPath cString] );
strcat( szString, "/" );
strcat( szString, [[oSelected title] cString] );
// Stop..
[self Cleanup];
// Start playing...
[self PlayAMovie:[NSString stringWithCString:szString]];
// Get the number of the item the user selected.
mPlaybackItem = itemNum;
// Update menu...
[oSelected setState:NSOnState];
}
-(void)PlayAMovie:(NSString*)file
{
mCurNSMovie = [[NSMovie alloc] initWithURL:[NSURL fileURLWithPath:file] byReference:NO];
mCurMovie = [mCurNSMovie QTMovie];
StartMovie(mCurMovie);
// Create the timer...
mPlaybackTimer = [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector: @selector(TimerProc:) userInfo:NULL repeats:YES];
}
-(void)TimerProc:(NSTimer*)timer
{
BOOL bGotoNext = false;
// Has the movie finished.
if ( mCurMovie != NULL )
bGotoNext = IsMovieDone(mCurMovie);
else
bGotoNext = true;
if ( bGotoNext )
{
[self Cleanup];
// Inc the playback item number...
mPlaybackItem++;
// Should we start again...
if ( mPlaybackItem >= [mSubMenu numberOfItems] )
mPlaybackItem = kFirstTrackItem;
// Start playing...
[self PlayMenuItemNumber:mPlaybackItem];
}
}
-(void)Cleanup
{
// Remove old tick mark..
if ( mPlaybackItem )
[[mSubMenu itemAtIndex:mPlaybackItem]setState:NSOffState];
if ( mCurMovie != NULL ) {
StopMovie(mCurMovie);
[mCurNSMovie release];
mCurMovie = NULL;
}
// Kill timmer.
if ( mPlaybackTimer != NULL ) {
[mPlaybackTimer invalidate];
mPlaybackTimer = NULL;
}
}
@end